package com.light.netty.client.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;

public class TcpBusinessClientHandler extends ChannelInboundHandlerAdapter {
    private String name;

    public TcpBusinessClientHandler(String name){
        this.name = name;
    }

    /**
     * 当Channel成功注册到EventLoop上时调用。
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TCP Client Channel registered: " + ctx.channel().id());
    }

    /**
     * 当Channel从EventLoop上取消注册时调用。
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TCP Client Channel unregistered: " + ctx.channel().id());
    }

    /**
     * 当Channel已经打开并且连接到对端时调用
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i = 0; i < 5000; i++) {
            int finalI = i;

            String msg = name + "TCP Client: Hello Server" + finalI;
            ctx.writeAndFlush(msg +"\r\n").addListener(future -> {
                if (future.isSuccess()) {
                    System.out.println(name+"TCP Client Message sent successfully");
                } else {
                    System.err.println(name+"TCP Client Failed to send message: " + future.cause());
                }
            });
        }
    }

    /**
     * 当Channel连接关闭时调用。
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TCP Client Channel inactive: " + ctx.channel().id());
    }

    /**
     * 当Channel中有数据可读时调用。
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println(name+"服务端确认消息: " + msg);
    }

    /**
     * 当Channel中的数据已经读取完毕时调用。
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TCP Client Channel read complete");
    }

    /**
     * 当Channel的可写状态发生改变时调用。
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
        System.out.println("TCP Client Channel writability changed: " + ctx.channel().isWritable());
    }

    /**
     * 用于处理用户自定义的事件。
     * @param ctx
     * @param evt
     * @throws Exception
     */
//    @Override
//    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
//        if (evt instanceof MyCustomEvent) {
//            MyCustomEvent event = (MyCustomEvent) evt;
//            System.out.println("Received custom event: " + event.getMessage());
//        }
//    }

    /**
     * 用于捕获和处理Channel中发生的异常。
     * @param ctx
     * @param cause
     * @throws Exception
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        // 处理异常情况
        cause.printStackTrace();
        ctx.close();
    }
}